package com.weetop.akly.activity.login;

import android.os.Bundle;
import android.text.Editable;
import android.text.TextUtils;
import android.text.method.HideReturnsTransformationMethod;
import android.text.method.PasswordTransformationMethod;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;

import com.blankj.utilcode.util.ToastUtils;
import com.qmuiteam.qmui.util.QMUIStatusBarHelper;
import com.weetop.akly.R;
import com.weetop.akly.activity.MainActivity;
import com.weetop.akly.appConfig.GlobalConstants;
import com.weetop.akly.base.BaseActivity;
import com.weetop.akly.base.mvp.BaseModel;
import com.weetop.akly.bean.LoginBean;
import com.weetop.akly.presenter.LoginPresent;
import com.weetop.akly.utils.BaseUtils;
import com.weetop.akly.utils.MMKVUtils;
import com.weetop.akly.utils.StarView.StringUtils;
import com.weetop.akly.view.LoginView;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.OnTextChanged;
import io.rong.imkit.RongIM;
import io.rong.imlib.RongIMClient;

/**
 * 登录界面
 */
public class LoginActivity extends BaseActivity<LoginPresent> implements LoginView {

    @BindView(R.id.iv_devide)
    ImageView ivDevide;
    @BindView(R.id.bt_login)
    Button btLogin;
    @BindView(R.id.et_acount)
    EditText etAcount;
    @BindView(R.id.et_psd)
    EditText etPsd;
    @BindView(R.id.iv_showpsd)
    ImageView ivShowpsd;
    private boolean isShow = false;
    private String account = "";
    private String psd = "";

    @Override
    protected LoginPresent createPresenter() {
        return new LoginPresent(this, LoginActivity.this);
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        ButterKnife.bind(this);
        QMUIStatusBarHelper.translucent(this);
        btLogin.setEnabled(false);
    }

    /**
     * 登录成功
     */
    @Override
    public void loginSuccess(BaseModel<LoginBean> o) {
        LoginBean loginBean = o.getData();
        initRongYunSDK(loginBean.getRy_token());
        MMKVUtils.saveUserToken(loginBean.getToken());
        MMKVUtils.saveRyToken(loginBean.getRy_token());
        MMKVUtils.putObject(GlobalConstants.UserInfo,loginBean.getInfo());
        ToastUtils.showShort("登录成功");
        BaseUtils.toActivity(mActivity, MainActivity.class);
        finish();
    }

    private void initRongYunSDK(String ry_token) {
        if (TextUtils.isEmpty(ry_token)) {
            return;
        }
        //融云   后期接入
        RongIM.connect(ry_token, new RongIMClient.ConnectCallback() {
            @Override
            public void onTokenIncorrect() {

            }

            @Override
            public void onSuccess(String userid) {
            }

            @Override
            public void onError(RongIMClient.ErrorCode errorCode) {
            }
        });
    }

    @OnClick({R.id.iv_showpsd, R.id.bt_login})
    public void onViewClicked(View view) {
        if (BaseUtils.fastClick())
            switch (view.getId()) {
                case R.id.iv_showpsd:
                    //密码显示or影藏
                    showHidePassword();
                    break;
                case R.id.bt_login:
                    //登录
                    if (StringUtils.isNotEmpty(etAcount.getText().toString()) && StringUtils.isNotEmpty(etPsd.getText().toString())) {
                        mPresenter.toLogin(account, psd);
                    } else {
                        ToastUtils.showShort("账号密码不能为空");
                    }
                    break;
            }
    }

    @OnTextChanged(value = R.id.et_acount, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void onAccountChanged(Editable s) {
        Log.e("TAG", "onTextChanged , s = " + s);
        account = s.toString().trim();
        if (!TextUtils.isEmpty(account) && !TextUtils.isEmpty(psd) && account.length() >= 2 && psd.length() >= 6) {
            btLogin.setEnabled(true);
        } else {
            btLogin.setEnabled(false);
        }
    }

    @OnTextChanged(value = R.id.et_psd, callback = OnTextChanged.Callback.AFTER_TEXT_CHANGED)
    void onPsdChanged(Editable s) {
        Log.e("TAG", "onTextChanged , s = " + s);
        psd = s.toString().trim();
        if (!TextUtils.isEmpty(account) && !TextUtils.isEmpty(psd) && account.length() >= 2 && psd.length() >= 6) {
            btLogin.setEnabled(true);
        } else {
            btLogin.setEnabled(false);
        }
    }

    private void showHidePassword() {
        isShow = !isShow;
        if (isShow) {
            ivShowpsd.setImageResource(R.mipmap.eye);
            etPsd.setTransformationMethod(HideReturnsTransformationMethod.getInstance());
        } else {
            ivShowpsd.setImageResource(R.mipmap.closeeye);
            etPsd.setTransformationMethod(PasswordTransformationMethod.getInstance());
        }
        etPsd.setSelection(etPsd.getText().toString().length());
    }


}
